<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>socket</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4>NAME</h4><blockquote>
socket - create an endpoint for communication
</blockquote><h4>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="syssocket.h.html">sys/socket.h</a>&gt;

int socket(int <I>domain</I>, int <I>type</I>, int <I>protocol</I>);
</code>
</pre>
</blockquote><h4>DESCRIPTION</h4><blockquote>
The
<i>socket()</i>
function creates an unbound socket in a communications domain, and returns a
file descriptor that can be used in later function calls that operate on
sockets.
<p>
The function takes the following arguments:
<dl compact>

<dt><I>domain</I><dd>Specifies the communications domain in which a socket is to be created.

<dt><I>type</I><dd>Specifies the type of socket to be created.

<dt><I>protocol</I><dd>Specifies a particular protocol to be used with the socket.
Specifying a <I>protocol</I> of 0 causes
<i>socket()</i>
to use an unspecified default protocol appropriate for the
requested socket type.

</dl>
<p>
The <I>domain</I> argument specifies the address family used in the
communications domain.  The address families supported by the system are
implementation-dependent.
<p>
The
<i><a href="syssocket.h.html">&lt;sys/socket.h&gt;</a></i>
header defines at least the following values for the
<I>domain</I>
argument:
<dl compact>

<dt>AF_UNIX<dd>File system pathnames.

<dt>AF_INET<dd>Internet address.

</dl>
<p>
The <I>type</I> argument specifies the socket type, which determines
the semantics of communication over the socket.  The socket types
supported by the system are implementation-dependent.  Possible
socket types include:
<dl compact>

<dt>SOCK_STREAM<dd>Provides sequenced, reliable, bidirectional, connection-mode byte streams,
and may provide a transmission mechanism for out-of-band data.

<dt>SOCK_DGRAM<dd>Provides datagrams, which are connectionless-mode, unreliable messages of fixed
maximum length.

<dt>SOCK_SEQPACKET<dd>Provides sequenced, reliable, bidirectional, connection-mode transmission
path for records.  A record can be sent using one or more output operations
and received using one or more input operations, but a single operation never
transfers part of more than one record.  Record boundaries are visible to
the receiver via the MSG_EOR flag.

</dl>
<p>
If the <I>protocol</I> argument is non-zero, it must specify a
protocol that is supported by the address family.  The protocols
supported by the system are implementation-dependent.
<p>
The process may need to have appropriate privileges to use the
<i>socket()</i>
function or to create some sockets.
</blockquote><h4>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>socket()</i>
returns a nonnegative integer, the socket file descriptor.  Otherwise a value
of -1 is returned and <I>errno</I> is set to indicate the error.
</blockquote><h4>ERRORS</h4><blockquote>
The
<i>socket()</i>
function will fail if:
<dl compact>

<dt>[EAFNOSUPPORT]<dd>The implementation does not support the specified address family.

<dt>[EMFILE]<dd>No more file descriptors are available for this process.

<dt>[ENFILE]<dd>No more file descriptors are available for the system.

<dt>[EPROTONOSUPPORT]<dd><br>
The protocol is not supported by the address family, or
the protocol is not supported by the implementation.

<dt>[EPROTOTYPE]<dd>The socket type is not supported by the protocol.

</dl>
<p>
The
<i>socket()</i>
function may fail if:
<dl compact>

<dt>[EACCES]<dd>The process does not have appropriate privileges.

<dt>[ENOBUFS]<dd>Insufficient resources were available in the system to perform the operation.

<dt>[ENOMEM]<dd>Insufficient memory was available to fulfill the request.

<dt>[ENOSR]<dd>There were insufficient STREAMS resources available for the operation to
complete.

</dl>
</blockquote><h4>APPLICATION USAGE</h4><blockquote>
The documentation for specific address families specify which protocols each
address family supports.  The documentation for specific protocols specify
which socket types each protocol supports.
<p>
The application can determine if an address family is supported
by trying to create a socket with
<I>domain</I>
set to the protocol in question.
</blockquote><h4>SEE ALSO</h4><blockquote>
<i><a href="accept.html">accept()</a></i>,
<i><a href="bind.html">bind()</a></i>,
<i><a href="connect.html">connect()</a></i>,
<i><a href="getsockname.html">getsockname()</a></i>,
<i><a href="getsockopt.html">getsockopt()</a></i>,
<i><a href="listen.html">listen()</a></i>,
<i><a href="recv.html">recv()</a></i>,
<i><a href="recvfrom.html">recvfrom()</a></i>,
<i><a href="recvmsg.html">recvmsg()</a></i>,
<i><a href="send.html">send()</a></i>,
<i><a href="sendmsg.html">sendmsg()</a></i>,
<i><a href="setsockopt.html">setsockopt()</a></i>,
<i><a href="shutdown.html">shutdown()</a></i>,
<i><a href="socketpair.html">socketpair()</a></i>,
<i><a href="netinetin.h.html">&lt;netinet/in.h&gt;</a></i>,
<i><a href="syssocket.h.html">&lt;sys/socket.h&gt;</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
